package com.ruoyi.controller;


import com.ruoyi.domain.Document;
import com.ruoyi.platform.annotation.Log;
import com.ruoyi.platform.core.controller.BaseController;
import com.ruoyi.platform.core.domain.AjaxResult;
import com.ruoyi.platform.core.page.TableDataInfo;
import com.ruoyi.platform.enums.BusinessType;
import com.ruoyi.platform.utils.poi.ExcelUtil;
import com.ruoyi.service.IDocumentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文档Controller
 *
 * @author tp
 * @date 2022-11-02
 */
@RestController
@RequestMapping("/admin/document")
@Api(tags = {"文档服务"})
public class DocumentController extends BaseController {

    @Autowired
    private IDocumentService documentService;

    /**
     * 查询文档列表
     */
    @PreAuthorize("@ss.hasPermi('admin:document:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询文档列表", httpMethod = "GET", notes = "查询文档列表")
    public TableDataInfo list(Document document) {
        startPage();
        List<Document> list = documentService.selectDocumentList(document);
        return getDataTable(list);
    }

    /**
     * 导出文档列表
     */
    @PreAuthorize("@ss.hasPermi('admin:document:export')")
    @Log(title = "文档", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出文档列表", httpMethod = "POST", notes = "导出文档列表")
    public void export(HttpServletResponse response, Document document) {
        List<Document> list = documentService.selectDocumentList(document);
        ExcelUtil<Document> util = new ExcelUtil<>(Document.class);
        util.exportExcel(response, list, "文档数据");
    }

    /**
     * 获取文档详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:document:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取文档详细信息", httpMethod = "GET", notes = "获取文档详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(documentService.selectDocumentById(id));
    }


    /**
     * 新增文档
     */
    @PreAuthorize("@ss.hasPermi('admin:document:add')")
    @Log(title = "文档", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增文档", httpMethod = "POST", notes = "新增文档")
    public AjaxResult add(@RequestBody Document document) {
        return toAjax(documentService.insertDocument(document));
    }

    /**
     * 修改文档
     */
    @PreAuthorize("@ss.hasPermi('admin:document:edit')")
    @Log(title = "文档", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改文档", httpMethod = "PUT", notes = "修改文档")
    public AjaxResult edit(@RequestBody Document document) {
        return toAjax(documentService.updateDocument(document));
    }

    /**
     * 删除文档
     */
    @PreAuthorize("@ss.hasPermi('admin:document:remove')")
    @Log(title = "文档", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除文档", httpMethod = "DELETE", notes = "删除文档")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(documentService.deleteDocumentByIds(ids));
    }

    /**
     * 根据文档名模糊查询文档
     *
     * @param name
     * @return
     */
    @GetMapping("/getDocumentByName")
    public AjaxResult searchDocumentByName(@RequestParam(value = "name", required = false) String name) {
        return AjaxResult.success(documentService.getDocumentListByName(name));
    }

    @GetMapping("/querySearchDocument")
    public AjaxResult querySearchDocumentByName(@RequestParam("name")String name){
        return AjaxResult.success(documentService.querySearchDocument(name));
    }

}
